from lxml import etree
import PySimpleGUI as sg
import os

#设定目标目录,必须是双斜杠，结尾必须要双斜杠
location="c:\\temp\\test\\"

order_dest="misc"  #"misc"
chip_dest="cd"  #"cd"
production_dest="pd" #"pd"
response_dest="rd"   #"rd"


layout = [[sg.Text('订单号   '),sg.Input()],
          [sg.Text('html页面'),sg.Input(), sg.FileBrowse()],
          [sg.OK(), sg.Cancel()]]

window = sg.Window('html页面分析',layout,keep_on_top=True)

event, values = window.read()
window.close()
file=values["Browse"]
order_num=values[0]
with open(order_num+'.txt','w') as f:
    f.write('')
def write(words):
    with open(order_num+'.txt','a') as f:
        f.write(words+'\n')
def html(filename):

    with open(filename,'r') as f:

        text=f.read()
    
    tree = etree.HTML(text)

    html=etree.HTML(text) #初始化生成一个XPath解析对象

    result=etree.tostring(html,encoding='utf-8')
    #sg.Print("REM ###########################生成批处理bat文件###################################")
    #write("REM ###########################生成批处理bat文件###################################")
    #sg.Print('REM 文件名:',filename)
    write('REM ####开始html文件名:{}  请保存为BAT文件'.format(filename))
    global order_dest,chip_dest,production_dest,response_dest
    order_dest=location+order_num+order_dest
    chip_dest= location+order_num+chip_dest
    production_dest=location+order_num+production_dest
    response_dest=location+order_num+response_dest
    
    #sg.Print('REM 新建文件夹:')
    write('REM 新建文件夹:')
    #sg.Print('mkdir '+order_dest)
    write('mkdir '+order_dest)
    #sg.Print('mkdir '+chip_dest)
    write('mkdir '+chip_dest)
    #sg.Print('mkdir '+production_dest)
    write('mkdir '+production_dest)
    #sg.Print('mkdir '+response_dest)
    write('mkdir '+response_dest)
    
    #订单
    order=html.xpath('//b[contains(text(),"Provider: ")]/../text()')


    #sg.Print('REM Order file:')
    write('')
    write('REM #######Order file######:')
    #订单要处理的命令
    #sg.Print('copy /Y ',order[2]+'\\'+order[3], order_dest)
    write('copy /Y '+order[2]+'\\'+order[3]+" "+order_dest)
    #sg.Print('REM Profile structure file:')
    write('')
    write('REM ######Profile structure file:#########')
    #订单要处理的命令
    #sg.Print('copy /Y ',order[5]+'\\'+order[6], order_dest)
    write('copy /Y '+order[5]+'\\'+order[6]+" "+order_dest)
    #芯片
    write('')
    chip=html.xpath('//b[contains(text(),"Chip data path:")]/../text()')

     

    for item in html.xpath('//th[contains(text(),"Input file")]/../../../tbody/tr'):

        #sg.Print('REM 芯片信息:',chip[0]+'\\'+item[1].text)
        write('REM #############Chip file:'+chip[0]+'\\'+item[1].text)
        #芯片要处理的命令
        #sg.Print('copy /Y ',chip[0]+'\\'+item[1].text,chip_dest)
        write('copy /Y '+chip[0]+'\\'+item[1].text+" "+chip_dest)

    product=html.xpath('//b[contains(text(),"Production data path")]/../text()')

     
    #生产
    write('')
    for item in html.xpath('//th[contains(text(),"Chip file")]/../../../tbody/tr'):

        #sg.Print('REM 生产信息:',product[2]+'\\'+item[1].text)
        write('REM ###########Production file: '+product[2]+'\\'+item[1].text)
        #生产的命令
        #sg.Print('copy /Y ',product[2]+'\\'+item[1].text,production_dest)
        write('copy /Y '+product[2]+'\\'+item[1].text+" "+production_dest)
    #回复数据
    write('')
    Response_config_file_path=html.xpath('//b[contains(text(),"Response config file path")]/../text()[3]')[0]


    len_response=len(html.xpath('//b[contains(text(),"Input file path:")]'))
    len_response_files_one_time=len(html.xpath('//b[contains(text(),"Input file path:")]/../following-sibling::table[{}]/tbody/tr'.format(len_response)))
    for i in range(1,len_response+1):
        #sg.Print("REM 回复数据:",html.xpath('//b[contains(text(),"Input file path:")]/../../h3/u/text()')[i-1])
        write("REM ####回复数据:"+html.xpath('//b[contains(text(),"Input file path:")]/../../h3/u/text()')[i-1])
        
        config_file=html.xpath('//b[contains(text(),"Hash value of config file")]/../text()[2]')[i-1]
        #sg.Print("copy /Y ",Response_config_file_path+'\\'+config_file,order_dest)
        write("copy /Y "+Response_config_file_path+'\\'+config_file+" "+order_dest)
     
        Response=html.xpath('//b[contains(text(),"Input file path:")]/../following-sibling::table[{}]/tbody/tr/td[1]'.format(i))

        for y in range(len_response_files_one_time):
            #回复数据的命令
            #sg.Print("copy /Y ",Response[y].text, response_dest)
            write("copy /Y "+Response[y].text+" "+response_dest)
    #sg.Print("REM ##########################结束######################################")
    write('')
    write("REM ##########################结束######################################")
    #sg.Print('pause')
    write('pause')


html(file)
os.system('start {}.txt'.format(order_num))
